package com.travel.server.manager.impl.command.question;

import com.travel.common.CommonConstant;
import com.travel.common.util.CollectionUtil;
import com.travel.server.manager.dto.request.question.UpdateQuestionRequest;
import com.travel.server.manager.dto.request.question.UpdateQuestionRequest.QuestionItem;
import com.travel.server.manager.dto.response.EmptyResponse;
import com.travel.server.manager.impl.command.AbstractCommand;
import com.travel.server.manager.impl.dal.QuestionnaireDal;
import com.travel.server.manager.impl.dal.QuestionnaireDetailDal;
import com.travel.server.manager.impl.dal.QuestionnaireDetailDal.QuestionnaireDetailParam;
import com.travel.server.manager.model.QuestionnaireDetailInfo;
import com.travel.server.manager.model.QuestionnaireInfo;
import com.travel.server.manager.model.UserInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author: LiZhenhui
 * @Date: 2018\11\15 0015 22:24
 * @Desc:
 */
@Component
@Slf4j
public class UpdateQuestionCommand extends AbstractCommand<UpdateQuestionRequest, EmptyResponse> {

    @Autowired
    private QuestionnaireDal questionnaireDal;

    @Autowired
    private QuestionnaireDetailDal questionnaireDetailDal;

    @Override
    protected void checkInput(UpdateQuestionRequest request) {

    }

    @Override
    public EmptyResponse handle(UpdateQuestionRequest request) {
        UserInfo userInfo = getUserInfo();
        Date now = new Date();

        Integer id = request.getId();

        QuestionnaireInfo questionnaireInfo;
        if(id == null){
            questionnaireInfo = new QuestionnaireInfo();
            questionnaireInfo.setState(CommonConstant.YES);
            questionnaireInfo.setCreateBy(userInfo.getName());
            questionnaireInfo.setCreateTime(now);
        }else {
            questionnaireInfo = questionnaireDal.selectById(id);
        }

        questionnaireInfo.setQuestionnaireDescribe(request.getDescribe());
        questionnaireInfo.setBackGroundUrl(request.getBackGroundUrl());
        questionnaireInfo.setQuestionnaireName(request.getName());
        questionnaireInfo.setQuestionnaireTitle(request.getTitle());
        questionnaireInfo.setUpdateBy(userInfo.getName());
        questionnaireInfo.setUpdateTime(now);

        id = questionnaireDal.insertOrUpdate(questionnaireInfo);

        List<QuestionItem> questionList = request.getQuestionList();
        if(!CollectionUtil.isEmpty(questionList)){
            updateDetail(questionList, id, userInfo);
        }

        return new EmptyResponse();
    }

    private void updateDetail(List<QuestionItem> questionList, Integer questionId, UserInfo userInfo){
        QuestionnaireDetailParam param = new QuestionnaireDetailParam();
        param.setQuestionnaireId(questionId);
        List<QuestionnaireDetailInfo> list = questionnaireDetailDal.selectByExample(param);
        List<Integer> updateIdList = new ArrayList<>();

        Date now = new Date();
        for(QuestionItem item : questionList){
            Integer id = item.getId();
            QuestionnaireDetailInfo detail;
            if(id == null){
                detail = new QuestionnaireDetailInfo();
                detail.setQuestionnaireId(questionId);
                detail.setCreateBy(userInfo.getName());
                detail.setState(CommonConstant.YES);
                detail.setCreateTime(now);
            }else{
                updateIdList.add(id);
                detail = questionnaireDetailDal.selectById(id);
            }
            detail.setQuestion(item.getQuestion());
            detail.setAnswerList(item.getAnswerList());
            detail.setQuestionType(item.getType());
            detail.setUpdateBy(userInfo.getName());
            detail.setUpdateTime(now);
            questionnaireDetailDal.insertOrUpdate(detail);
        }

        if(!CollectionUtil.isEmpty(list)){
            for(QuestionnaireDetailInfo detail : list){
                boolean delete = true;
                for(Integer id : updateIdList){
                    if(id.equals(detail.getId())){
                        delete = false;
                        break;
                    }
                }
                if(delete){
                    detail.setState(CommonConstant.NO);
                    detail.setUpdateTime(now);
                    detail.setUpdateBy(userInfo.getName());
                    questionnaireDetailDal.insertOrUpdate(detail);
                }
            }
        }
    }
}
